package com.java.cloud.order.lb;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Ribbon手写轮询算法
 *
 * @author jiangli
 * @since 2020/3/10 16:05
 **/
@Component
public class MyLB implements LoadBalancer {
	private AtomicInteger atomicInteger = new AtomicInteger(0);

	/**
	 * 负载均衡算法:rest接口第几次请求数%服务器集群总数量=实际调用服务器位置下标,每次服务重启动后rest接口计数从1开始.
	 */
	@Override
	public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
		int index = getAndIncrement() % serviceInstances.size();
		return serviceInstances.get(index);
	}

	private int getAndIncrement() {
		int current;
		int next;
		do {
			current = this.atomicInteger.get();
			// 超过最大值,为0,重新计数 2147483647 Integer.MAX_VALUE
			next = current >= 2147483647 ? 0 : current + 1;
			// 自旋锁
		} while (!atomicInteger.compareAndSet(current, next));
		return next;
	}
}